/*
 * Utils 对象
 * @Author:Mr.Alex
 * @Email:<mr.wang@wangqiqiang.com>
 * @version:1.0.0
 * @date:2018-03-06 18:15:19
 */
(function(window,undefined){
	var
		_Utils = window.Utils,
		Utils = function(selector, context){
			return new Utils.fn.init(selector, context);
		};
		
	var
		selector,context;
		
	Utils.fn = Utils.prototype;
	
	Utils.fn.init = function(selector, context){
		this.selector = selector;
		
		if( context ){
			this.context = context;
		}else{
			this.context = window.document;
		}
		
		return this;
	}
	
	Utils.fn.init.prototype = Utils.fn;

	Utils.extend = Utils.fn.extend = function(){
		var
			target = arguments[0] || {},
			i = 1,
			length = arguments.length,
			options;
			
		var
			isObject = function(target){
				return typeof target === "object";
			},
			isFunction = function(target){
				return typeof target === "function";
			};
			
		if( isObject(target) === false && isFunction(target) === false ){
			target = {};
		}
		
		if( i === length ){
			target = this;
			i--;
		}
		
		for(i;i < length;i++)
		{
			options = arguments[i];
			
			if( options === null ){
				continue;
			}
			
			for (var name in options )
			{
				if( target === options[name] ){
					continue;
				}
				
				target[name] = options[name];
			}
		}
		
		return target;
	};
	
	window.Utils = Utils;
	
})(window);

Utils.extend({
	isBoolean:function(mix){
		return typeof mix === "boolean";
	},
	isObject:function(mix)
	{
		return typeof mix === "object";
	},
	isFunction:function(mix){
		return typeof mix === "function";
	},
	isArray:function(mix){
		return mix instanceof Array;
	},
	/*
	 * 移交控制权
	 */
	noConflict:function(deep){
		window.Utils = _Utils;
		return Utils;
	},
	/*
	 * Utils.namespace('a.b.c','x.y.z');
	 */
	namespace:function(){
		var
			target = arguments,
			callback = new Array();
		
		for(var i = 0; i < target.length; i++)
		{
			var direction = target[i].split(".");
			
			var origin = window;
			
			for (var k = 0; k < direction.length; k++)
			{
				origin[direction[k]] = origin[direction[k]] || {};
				
				origin = origin[direction[k]];
			}
			
			callback.push(origin);
		}
		
		return callback;
	},
	/*
	 * var UI = Utils.define('home.user','home.manager','home.admin',function(user,manager,admin){
	 * 	user.log = function(){}
	 * 	manager.log = function(){}
	 * 	admin.log = function(){}
	 * });
	 */
	define:function(){
		var target,callback,
			isString = Array.prototype.every.call(arguments,function(item){
				return typeof item === 'string';
			});
			
		if( isString ){
			target = this.namespace.apply(this,arguments);
		}else{
			callback = Array.prototype.pop.call(arguments,-1);
			
			target = this.namespace.apply(this,arguments);
			
			callback.apply(this,target);
		}
		
		return target;
	},
	/*
	 * Cookie 操作
	 * Cookie 操作需要依赖 jquery.cookie 库
	 * @Author:Mr.Alex
	 * @Email:<mr.wang@wangqiqiang.com>
	 * @version:1.0.0
	 * @date:2017-11-14 18:19:36
	 */
	Cookie:{
		add:function(key, value, options){
			return $.cookie(key, value, options);
		},
		remove:function(key,options){
			return $.cookie(key, null, options);
		},
		get:function(key){
			return $.cookie(key);
		},
	},
	/*
	 * WebSocket 封装
	 * @Author:Mr.Alex
	 * @Email:<mr.wang@wangqiqiang.com>
	 * @version:1.0.0
	 * @date:2017-11-15 00:06:07
		var ws = new Utils.WebSocket('ws://123.207.167.163:9010/ajaxchattest');
			ws.onopen = function(event){
				console.log(event);
				ws.send(456)
			};
			ws.onmessage = function(event){
				console.log(event);
			};
			ws.onerror = function(event){
				console.log(event);
			};
			ws.onclose = function(event){
				console.log(event);
			};
	 */
	WebSocket:function(){
		var ws = {
			isSupported:false,
		};
		
		if( window.WebSocket ){
			ws = WebSocket.apply(this,arguments);
			ws.isSupported = true;
		}
		
		return ws;
	},
});